package com.demo.lock;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable {
    public static void main(String[] args) throws Exception {
        /**
         * new Thread().start();
         * new Thread(new Runnable()).start();
         *      Runnable有一个实现类FutureTask
         * new Thread(new FutureTask(Callable callable)).start();
         */
        MyCallAble myCallAble = new MyCallAble();
        //适配类，因为callable有返回值，使用futuretask来接收
        FutureTask futureTask = new FutureTask(myCallAble);
        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start(); //结果会缓存
        //get方法可能会阻塞，可使用异步通信
        String value = (String) futureTask.get();
        System.out.println("value = " + value);
    }
}

class MyCallAble implements Callable<String>{
    /*
    1. 有返回值
    2. 抛异常
     */
    @Override
    public String call() throws Exception {
        System.out.println("call()-------");
        return "123456";
    }
}
